package com.salinity.kun.task;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.salinity.kun.algorithm.harmonic.ConstantParamHelper;
import com.salinity.kun.algorithm.harmonic.TideSymbol;
import com.salinity.kun.helper.BaseHydrologyDataHelper;
import com.salinity.kun.helper.ConstantHelper;
import com.salinity.kun.model.StationDateData;
import com.salinity.kun.util.DateUtil;
import com.salinity.kun.util.PathUtil;

/**
 * 组织样本数据，进行主成分分析
 * 
 * @author Administrator
 *
 */
public class PCATestTask implements ITask {

	@Override
	public boolean run() throws IOException, ParseException {

		return false;
	}

	public static void main(String[] args) {
		int hours;
		int rawDeltaT = 60;
		List<TideSymbol> tideSymbolList = new ArrayList<>();
		tideSymbolList.add(TideSymbol.M2);
		tideSymbolList.add(TideSymbol.S2);
		tideSymbolList.add(TideSymbol.N2);
		//tideSymbolList.add(TideSymbol.K2);
		
		tideSymbolList.add(TideSymbol.K1);
		tideSymbolList.add(TideSymbol.O1);
		//tideSymbolList.add(TideSymbol.P1);
		tideSymbolList.add(TideSymbol.Q1);
		
		tideSymbolList.add(TideSymbol.M4);
		tideSymbolList.add(TideSymbol.MS4);
		tideSymbolList.add(TideSymbol.M6);
		hours = 240;
		SimpleDateFormat sdf = ConstantHelper.getDateFormat();
		Date startDate,endDate;
		int i, j,k, m,n,colLen ;
		double val, w, ti,deltaT = 1 / 60f,tempRowParams[];
		List<StationDateData> lvTideList,denseDataList;
		Map<TideSymbol, Double> angleMap = ConstantParamHelper.getAngularVelocity().getAngel();
		try {
			startDate = sdf.parse("2005/1/1  0:00:00");
			endDate = sdf.parse("2005/1/31  0:00:00");
			endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(endDate).minusHours(hours));
			Path inputPath = Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\绿华山\\LvhuaShan_1.csv");
			Path outputPath = Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\test\\PCR_mid\\9\\240");
			Path outputFile;
			DecimalFormat df = new DecimalFormat("#.0000");
			
			Calendar calendar = DateUtil.Date2Calendar(startDate);
			
			while(!calendar.getTime().after(endDate)) {
				lvTideList = BaseHydrologyDataHelper.getTidesFromFile(inputPath, calendar.getTime(), rawDeltaT,
						hours);

				// 加密插值
				denseDataList = BaseHydrologyDataHelper.doProportionalInterpolation(lvTideList,
						rawDeltaT, 60);

				n = denseDataList.size();
				m = tideSymbolList.size();
				colLen = 2 * m + 1;
				ti = 0.0;
				tempRowParams = new double[colLen];
				tempRowParams[0] = 1.0;
				outputFile = outputPath.resolve("input_"+(calendar.get(Calendar.MONTH)+1)+"_"+calendar.get(Calendar.DAY_OF_MONTH)+".csv");
				
				PathUtil.preparePath(outputFile);
				
				try (BufferedWriter bw = Files.newBufferedWriter(outputFile, StandardOpenOption.WRITE)) {
					
					for (i = 0; i < colLen; i++) {
						bw.write("x" + i + ",");
					}
					bw.write("y");

					bw.newLine();
					for (i = 0; i < n; i++) {
						val = denseDataList.get(i).getValue();
						ti = i * deltaT;
						for (j = 0; j < m; j++) {
							w = angleMap.get(tideSymbolList.get(j)) * Math.PI / 180;

							// a(1~m)
							tempRowParams[j + 1] = Math.cos(w * ti);

							// b(1~m)
							tempRowParams[j + 1 + m] = Math.sin(w * ti);
						}

						for (k = 0; k < colLen; k++) {
							bw.write(df.format(tempRowParams[k]) + ",");
						}
						bw.write(df.format(val) + ",");
						bw.newLine();
					}

					System.out.println("complete...");

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(calendar.getTime());
				calendar.add(Calendar.DAY_OF_MONTH, 1);
	
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
